package contentSpider;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class ZHHant {

	/**
	 * @param args
	 */
	public String tChinese(String sChinese) {
		final WebClient webClient = new WebClient();
		webClient.setJavaScriptEnabled(false);
		webClient.setCssEnabled(false);
		HtmlPage convertHtml;
		String tChinese = null;
		try {
			convertHtml = webClient.getPage("http://" + fyHost + "/toBig5/convert.html");
			// Get the form that we are dealing with and within that form,
			// find the submit button and the field that we want to change.
			List list = convertHtml.getForms();
			HtmlForm form = null;
			for (Iterator iter = list.iterator(); iter.hasNext();) {
				form = (HtmlForm) iter.next();
			}
			HtmlSubmitInput button = form.getInputByName("fy");
			HtmlTextInput textField = form.getInputByName("data");
			textField.setValueAttribute(sChinese);
			final HtmlPage fyPage = button.click();
			// System.out.println(page2.getBody().asXml().replaceAll(">\\s*",
			// ">").replaceAll("\\s*<", "<"));
			tChinese = fyPage.getBody().asXml().replaceAll(">\\s+", ">").replaceAll("<body>", "").replaceAll("</body>", "").replaceAll("\n", "").replaceAll("\\s{3,200}", "  ");

		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tChinese;
	}
	
	String fyHost = null;

	public void setFyHost(String fyHost) {
		this.fyHost = fyHost;
	}
	
}
